--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 登录处理

local skynet = require "skynet"

local Login = class("Login")

--构造函数
function Login:ctor(message_dispatch)

	self.message_dispatch = message_dispatch
	self.server_name = skynet.getenv("server_name")

	self:register()
end

--注册消息
function Login:register()	
	self.message_dispatch:registerSelf("userLoginAuthReq", handler(self,self.userLoginAuthReq))
	self.message_dispatch:registerSelf("kick_user", handler(self,self.kickUser))

end

function Login:start()
	self.node_message = global.node_message
	self.session_manager = global.session_manager
	self.user_manager = global.user_manager
end

--登录token认证消息
function Login:userLoginAuthReq(fd, msg_data)

	local user_id = msg_data.user_id
	-- local token = msg_data.token

	local login_result = {
		error_code = 0,
		error_msg = "登陆成功！",
	}

	print("_____登录认证消息_________Login:onUserLoginAuth", msg_data, skynet.self())

	--验证通过
	--在redis生成一个user信息表更新信息
	--分配nodeserver
	--分配hallsever
	local user = self.user_manager:getUser(user_id)
	print("___user_________", user)
	if not user then
		--验证帐号密码
		local user_info = self.node_message:callDbProxy("select", user_id, "user", {{user_id=user_id}})
		print("_____get_UserInfo___from_db__",user_info)
		if user_info and next(user_info) then 
			--验证通过			
			local server_user = self.node_message:callDbRedis(user_id, "hgetall", "server_user:"..user_id)
			if server_user and next(server_user) then 				
				if server_user.gate_server ~= self.server_name then 
					--的别的节点登录了, 通知管理服踢人下线
					error_log("________的别的节点登录了__")
				end
			end
			-- server_user.user_id = user_id
			local old_user = self.user_manager:getUser(user_id)
			if old_user then 
				debug_log("______有旧用户________", old_user:getFD())
				self:closeSession(old_user:getFD())
			end

			--绑定会话
			self.session_manager:bindSession(fd, user_id)
			--给user分配server
			self:createUser(fd, user_id, user_info.user_name)						
			return {
				userLoginAuthRes = login_result
			}

		else
			return { userLoginAuthRes = {
				error_code = 1,
				error_msg = "登陆失败,取不到网关信息",
			}}
		end	
	end

	local old_fd = user:getFD()
	--同服务不用fd
	if old_fd and old_fd ~= fd then 
		self:closeSession(old_fd)		
	end
	--绑定会话
	self.session_manager:bindSession(fd, user_id)
	local user_data = self.node_message:callManager("assignment_server", user:getData())			
	user:setData(user_data)	
	self.node_message:callDbRedis(user_id, "hmset", "server_user:"..user_id, user:getData())

	return { userLoginAuthRes = login_result}
end

function Login:createUser(fd, user_id, user_name)
	--给user分配server	
	-- local user_data = self.node_message:callManager("assignment_server", server_user)			
	-- user_data.service = skynet.self()
	-- user_data.fd = fd
	-- user_data.user_name = user_name
	-- local user = User.new(user_id)
	-- user:setData(server_user)
	-- user:setData(user_data)	

	self.user_manager:createUser(user_id)
	local user = self.user_manager:getUser(user_id)
	local user_data = self.node_message:callManager("assignment_server", user:getData())			
	user_data.service = skynet.self()
	user_data.fd = fd
	user_data.user_name = user_name
	user:setData(user_data)		
	-- print("_____self.user_manager__", self.user_manager:getUserList())
	-- print("___11111___", xxx:getData())
	self.node_message:callDbRedis(user_id, "hmset", "server_user:"..user_id, user:getData())		
end

--关掉指定的fd
function Login:closeSession(old_fd)
	if not old_fd then 
		return 
	end
	local session = self.session_manager:getSession(old_fd)
	if not session then 
		return 
	end
	print("________异地登录___",old_fd)
	--解除绑定
	self.session_manager:unbindSession(old_fd)		
	self.node_message:sendService(session.gate, "kick", old_fd)
	--发送异地登录通知	
	local body = {
		error_code = 0;
		error_msg = "用户在别的地方登录！"
	}
	self.node_message:sendClient(session.session_id, session.contype, "kickUserNt", body)

end

function Login:kickUser(user_id, fd)
	self:closeSession(fd)
	self.user_manager:removeUser(user_id)
end


return Login